<?php
    class sessao extends database {

        function existe($login) {
            $response = false;
            if ($login != '') {
                $sql = "SELECT usu_id
                    FROM
                    usuarios
                    WHERE
                    usu_login = '".$login."'";
                $query = $this->sqlQuery($sql);
                if ($this->num_rows($query) > 0) {
                    $response = true;
                }
            }
            return $response;
        }

        function ativo($login) {
            $response = false;
            if ($login != '') {
                $sql = "SELECT
                    status_id
                    FROM
                    usuarios
                    WHERE
                    usu_login = '".$login."'";
                $query = $this->sqlQuery($sql);
                if ($this->num_rows($query) > 0) {
                    $row = $this->fetch_assoc($query);
                    if ($row["status_id"] == 1) {
                        $response = true;
                    }
                }
            }
            return $response;
        }
        
          function logado() {
            $sql = " SELECT COUNT(*) as logado FROM usuarios WHERE usu_id = '". $_SESSION['usu_id'] ."'";
            $query = $this->sqlQuery($sql);
            $obj = $this->fetch_object($query);
            return $obj->logado;
        }
        
               function usuarioLogado() {
            $sql = " SELECT usu_nome, usu_email from
                usuarios                 
                WHERE usu_id = '". $_SESSION['usu_id'] ."'";
            $query = $this->sqlQuery($sql);
            return $this->fetch_object($query);
        }

        public function login($login, $senha) {
            $response = "{success: false}";
            if (!$this->existe($login)) {
                $response = "{errors:[{id:'username', msg:'Usu&aacute;rio n&atilde;o encontrado.'}]}";
            }
            else if(!$this->ativo($login)) {
                $response = "{errors:[{id:'username', msg:'Essa conta n&atilde;o est&aacute; ativada.'}]}";
            } else {
                // check password
                $sql = "SELECT
                    usu_id,
                    usu_login
                    FROM
                    usuarios
                    WHERE
                    usu_login = '".$login."'
                    AND
                    usu_senha = '".$senha."'";
                if ($this->num_rows($query = $this->sqlQuery($sql)) < 1) {
                    $response = "{errors:[{id:'password', msg:'Senha incorreta'}]}";
                } else {
                    $obj = $this->fetch_object($query);

                        $_SESSION['usu_id'] = $obj->usu_id;
                        $_SESSION['usu_nome'] = $obj->usu_nome;
                        $_SESSION['niv_id'] = $obj->niv_id;
                        $response = "{success: true}";
                    
                }
            }
            return $response;
        }
		public function logout() {
            session_destroy();
            unset($_SESSION['usu_id']);
            unset($_SESSION['usu_nome']);
            unset($_SESSION['niv_id']);
        }
        
        public function forgot_password() {
           $response = "{success: false}";
           return $response;
        }
    }
?>
